<template>
  <div class="tag">
    <el-tag
      :key="tag.name"
      v-for="(tag, index) in tags"
      :closable="tag.name !== 'home'"
      :disabled-transition="false"
      :effect="$route.name === tag.name ? 'dark' : 'plain'"
      @click="changeMenu(tag)"
      @close="handleClose(tag, index)"
      >{{ tag.label }}</el-tag
    >
  </div>
</template>
<script>
// import Cookie from 'js-cookie'
import { useRouter, useRoute } from 'vue-router'
import { useStore } from 'vuex'

export default {
  setup() {
    const store = useStore()
    const router = useRouter()
    const route = useRoute()
    const tags = store.state.tabList
    const changeMenu = (item) => {
      router.push({ name: item.name })
    }
    const handleClose = (tag, index) => {
      let length = tags.length - 1
      // 处理vuex的tabList数组
      store.commit('CloseMenu', tag)
      // 判断当前路由不等于路由就停止
      if (tag.name !== route.name) {
        return
      }
      if (index === length) {
        router.push({
          name: tags[index - 1].name,
        })
      } else {
        router.push({
          name: tags[index].name,
        })
      }
    }
    return {
      tags,
      changeMenu,
      handleClose,
    }
  },
}
</script>
<style lang="less" scoped>
.tag {
  padding: 20px;
  width: 100%;
  .el-tag {
    margin-right: 15px;
    cursor: pointer;
  }
}
</style>
